﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace SemestralniPraceY35VAN
{
    class User
    {
        /// <summary>
        /// Properties of the user.
        /// </summary>
        private Hashtable properties;
        
        /// <summary>
        /// 
        /// </summary>
        public string ID { get { if (properties.ContainsKey("id")) return (string)properties["id"]; else return ""; } }

        /// <summary>
        /// 
        /// </summary>
        public string FirstName { get { if (properties.ContainsKey("firstname")) return (string)properties["firstname"]; else return ""; } }

        /// <summary>
        /// 
        /// </summary>
        public string LastName { get { if (properties.ContainsKey("lastname")) return (string)properties["lastname"]; else return ""; } }

        /// <summary>
        /// 
        /// </summary>
        public string Src { get { if (properties.ContainsKey("src")) return (string)properties["src"]; else return ""; } }

        /// <summary>
        /// 
        /// </summary>
        public Image Photo { get { if (properties.ContainsKey("photo")) return (Image)properties["photo"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public string Gender { get { if (properties.ContainsKey("gender")) return (string)properties["gender"]; else return ""; } }

        /// <summary>
        /// 
        /// </summary>
        public string Phone { get { if (properties.ContainsKey("phone")) return (string)properties["phone"]; else return ""; } }

        /// <summary>
        /// 
        /// </summary>
        public string Email { get { if (properties.ContainsKey("email")) return (string)properties["email"]; else return ""; } }

        /// <summary>
        /// 
        /// </summary>
        public Settings Setting { get { if (properties.ContainsKey("setting")) return (Settings)properties["setting"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public Status Status { get { if (properties.ContainsKey("status")) return (Status)properties["status"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public Checkin Checkin { get { if (properties.ContainsKey("checkin")) return (Checkin)properties["checkin"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public List<Badge> Badges { get { if (properties.ContainsKey("badges")) return (List<Badge>)properties["badges"]; else return null; } }

        /// <summary>
        /// 
        /// </summary>
        public List<Venue> Mayor { get { if (properties.ContainsKey("mayor")) return (List<Venue>)properties["mayor"]; else return null; } }

        /// <summary>
        /// Constructs an object representing a user.
        /// </summary>
        /// <param name="venue">XML node with information about the user</param>
        public User(XmlNode user)
        {
            properties = new Hashtable();
            
            if (user.Name == "user")
            {
                for (int i = 0; i < user.ChildNodes.Count; i++)
                {
                    switch (user.ChildNodes[i].Name)
                    {
                        case "photo":
                            properties.Add("src", user.ChildNodes[i].InnerText);
                            properties.Add(user.ChildNodes[i].Name, Downloader.DownloadImage(user.ChildNodes[i].InnerText));
                            break;
                        case "settings":
                            properties.Add(user.ChildNodes[i].Name, new Settings(user.ChildNodes[i]));
                            break;
                        case "status":
                            properties.Add(user.ChildNodes[i].Name, new Status(user.ChildNodes[i]));
                            break;
                        case "checkin":
                            properties.Add(user.ChildNodes[i].Name, new Checkin(user.ChildNodes[i]));
                            break;
                        case "badges":
                            List<Badge> badges = new List<Badge>();

                            foreach (XmlNode badge in user.ChildNodes[i].ChildNodes)
                            {
                                badges.Add(new Badge(badge));
                            }

                            properties.Add(user.ChildNodes[i].Name, badges);
                            break;
                        case "mayor":
                            List<Venue> mayor = new List<Venue>();

                            foreach (XmlNode venue in user.ChildNodes[i].ChildNodes)
                            {
                                mayor.Add(new Venue(venue));
                            }

                            properties.Add(user.ChildNodes[i].Name, mayor);
                            break;
                        default:
                            properties.Add(user.ChildNodes[i].Name, user.ChildNodes[i].InnerText);
                            break;
                    }
                }
            }
            else
            {
                throw new IOException("Not User!");
            }
        }
    }
}
